package com.cow.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cow.mapper.UserAddressMapper;
import com.cow.mapper.UserMapper;
import com.cow.pojo.User;
import com.cow.pojo.UserAddress;
import com.cow.pojo.UserAddressExample;
import com.cow.pojo.UserExample;
import com.cow.service.UserService;

/**
 * @author 光环and鬼泣
 * @version 2018年5月24日 上午10:39:25
 */
@Service("userService")
public class UserServiceImpl implements UserService {

	@Autowired
	private UserMapper userMapper = null;
	@Autowired
	private UserAddressMapper userAddressMapper = null;

	@Override
	public int insertUser(User user) throws Exception {
		int result = userMapper.insert(user);
		return result;
	}

	@Override
	public User getUser(String userName) throws Exception {
		UserExample example = new UserExample();
		UserExample.Criteria criteria = example.createCriteria();
		criteria.andUserNameEqualTo(userName);
		List<User> user = userMapper.selectByExample(example);
		if (user.size() == 0) {
			return null;
		} else {
			return user.get(0);
		}
	}

	@Override
	public User getUserbyId(String userId) throws Exception {
		User user = userMapper.selectByPrimaryKey(userId);
		return user;
	}

	@Override
	public int updataUser(User user) throws Exception {
		int result = userMapper.updateByPrimaryKey(user);
		return result;
	}

	@Override
	public int insertUseraddress(UserAddress userAddress) throws Exception {
		int result = userAddressMapper.insert(userAddress);
		return result;
	}

	@Override
	public List<UserAddress> getUseraddressList(String userId) throws Exception {
		UserAddressExample example = new UserAddressExample();
		UserAddressExample.Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		List<UserAddress> userAddressList = userAddressMapper.selectByExample(example);
		return userAddressList;
	}

	@Override
	public int updataUseraddress(UserAddress userAddress) throws Exception {
		int result = userAddressMapper.updateByPrimaryKey(userAddress);
		return result;
	}

	@Override
	public UserAddress getUseraddress(String addressId) throws Exception {
		UserAddress userAddress = userAddressMapper.selectByPrimaryKey(addressId);
		return userAddress;
	}

}
